<template>
  <div class="page-container">
    <van-nav-bar
      title="测试"
      left-text=""
      right-text=""
      left-arrow
      @click-left="onClickLeft"
    />
    <van-button @click="goMini">跳转小程序</van-button>

    <wx-open-launch-weapp
      id="launch-btn"
      username="gh_4e6c969491a9"
      path="/pages/index/index"
      @launch="handleLaunchFn"
      @error="handleErrorFn"
    >
      <!-- replace -->
      <template>
        <button
          style="
            width: 200px;
            height: 45px;
            text-align: center;
            font-size: 17px;
            display: block;
            margin: 0 auto;
            padding: 8px 24px;
            border: none;
            border-radius: 4px;
            background-color: #07c160;
            color: #fff;
          "
        >
          打开小程序
        </button>
      </template>
    </wx-open-launch-weapp>
  </div>
</template>

<script>
import { userInfo } from '@/api/user'
import {
  Button,
  Card,
  Tag,
  Toast,
  Swipe,
  SwipeItem,
  GoodsAction,
  Icon,
  RadioGroup,
  Radio
} from 'vant'
export default {
  name: 'Home',
  components: {
    [Button.name]: Button,
    [Card.name]: Card,
    [Tag.name]: Tag,
    [Toast.name]: Toast,
    [Swipe.name]: Swipe,
    [SwipeItem.name]: SwipeItem,
    [GoodsAction.name]: GoodsAction,
    [Icon.name]: Icon,
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio
  },
  data() {
    return {}
  },
  created() {},
  mounted() {
    // this.getShopWxConfig()
  },
  methods: {
    handleLaunchFn(e) {
      console.log('success成功', e)
    },
    handleErrorFn(e) {
      console.log('fail这里吗', e.detail)
    },
    goMini() {
      location.href = 'weixin://dl/business/?t=4sMUnubdrmq&a=1'
    },
    getShopWxConfig() {
      const url = window.location.href
      userInfo(url).then((res) => {
        const wx1 = window['wx']
        wx1.config({
          debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          appId: 'wxda711ece6bd54d98', // 必填，公众号的唯一标识
          timestamp: '1649765', // 必填，生成签名的时间戳
          nonceStr: 'test123', // 必填，生成签名的随机串
          signature: '20b83f50403e9755051caacd09d0213d97dec03a', // 必填，签名
          jsApiList: [], // 必填，需要使用的JS接口列表
          openTagList: ['wx-open-launch-weapp']
        })
        wx1.ready(function() {
          // console.log('res44444444')
        })
        wx1.error(function(err) {
          // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
          console.log('err44444444', err)
        })
      })
    },
    // 返回
    onClickLeft() {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="less" scoped>
.page-container {
  height: 100vh;
  padding-bottom: 60px;
  box-sizing: border-box;
  overflow: auto !important;
}
</style>
